<template>
  <view>
    <view class="goods-list">
      <view v-for="(item,i) in goodsList" :key="i" @click="goToDetail(item)">
       <my-goods :goods="item"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10,
        },
        goodsList: [],
        total: 0,
      };
    },
    methods: {
      //获取商品列表数据
      async getGoodsList(cb) {
        const {data:res} = await uni.$http.get('https://api-ugo-web.itheima.net/api/public/v1/goods/search',this.queryObj)
        if(res.meta.status !== 200) return uni.$showMsg()
        console.log(res);
        cb&&cb()
        this.goodsList = [...res.message.goods,...this.goodsList]
        this.total = res.message.total
      },
      onReachBottom() {
        //让页码值自增加一
        this.queryObj.pagenum++
        this.getGoodsList()
      },
      onPullDownRefresh() {
        //重置关键数据
        this.queryObj.pagenum = 1
        this.total = 0
        this.goodsList = []
        this.getGoodsList(() => {
          uni.stopPullDownRefresh()
        })
      },
      goToDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    },
    onLoad(options) {
      // console.log(options);
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      // console.log(this.queryObj);
      this.getGoodsList()
    },
  }
</script>

<style lang="scss">
  
</style>
